﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Hangjing.Common;
using Hangjing.SQLServerDAL;
using Hangjing.Model;

/// <summary>
/// 添加地址
/// </summary>
public partial class qy_54tss_Admin_Service_ajax_addaddress : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int DataId = Convert.ToInt32(WebUtility.InputText(Request["DataId"]));
        string Tel = WebUtility.InputText(Request["Tel"]);
        string UserName = Server.UrlDecode(WebUtility.InputText(Request["UserName"]));
        string Address = Server.UrlDecode(WebUtility.InputText(Request["Address"]));
        string lat = WebUtility.InputText(Request["lat"]);
        string lng = WebUtility.InputText(Request["lng"]);

        EAdminInfo admin = UserHelp.GetAdmin();

        int cityid = HjNetHelper.GetPostParam("cid",0);


        int isreg = 0;
        int userid = 0;
        ECustomerInfo adduser = new ECustomer().CheckUser(Tel,cityid, out isreg, out userid);



        EAddressInfo info = new EAddressInfo();
        info.DataID = DataId;
        info.Receiver = UserName;
        info.Address = Address;
        info.BuildingID = 0;
        info.Phone =Tel;
        info.Mobilephone = Tel;
        info.UserID = userid;
        info.AddTime = DateTime.Now;
        info.Pri = 0;
        info.Lat = lat;
        info.Lng = lng;
        int rs = -1;

        if (DataId == 0)
        {
            rs = new EAddress().Add(info);
            info.DataID = rs;
        }
        else
        {
            rs = new EAddress().Update(info);
        }
        Response.Clear();
        //反回1表示正确，-1表示出错
        if (rs <=0)
        {
            Response.Write(rs);//出错
        }
        else
        {

            WebUtility.FixsetCookie("used_addressid",info.DataID+"",1);

            StringBuilder sb = new StringBuilder();
            string addsql = " Mobilephone = '" + Tel + "'";
            IList<EAddressInfo> addlist = new EAddress().GetList(5, 1, addsql, "pri", 1);
            if (addlist.Count > 0)
            {
                EAddressInfo addmodel = new EAddressInfo();
                addmodel.Receiver = "新地址";
                addmodel.Address = "";
                addmodel.BuildingName = "";
                addlist.Add(addmodel);
            }
            int i = 0;
            foreach (EAddressInfo item in addlist)
            {
                sb.Append("<li><input type=\"radio\" name=\"addressradio1\" id=\"addrlist_" + item.DataID + "\" ");
                if (i == 0)
                {
                    sb.Append("checked ");

                    sb.Append(" class=\"radio1 first_addr\"");
                }
                else
                {
                    sb.Append(" class=\"radio1 \"");
                }
                sb.Append(" onclick=\"setaddress(this); \"");
                sb.Append(" value=\"" + item.Receiver + "^" + item.Address + "^" + item.Lat + "^" + item.Lng + "^" + item.DataID + "^" + item.BuildingID + "\" />");

                sb.Append("<label for=\"addrlist_" + item.DataID + "\">" + item.Receiver + " " + item.Address + "</label>");
                sb.Append("</li>");
                i++;
            }
            Response.Write(sb.ToString());
        }

        Response.End();
    }
}
